package com.scalar.chiptrack.operations.shippingRequest.actionCommands;

import com.scalar.chiptrack.actioncommands.ActionCommand;
import com.scalar.chiptrack.operations.shippingRequest.forms.ShippingRequestForm;
import com.scalar.chiptrack.operations.shippingRequest.ShippingRequest;
import com.scalar.chiptrack.users.User;
import com.scalar.chiptrack.users.Functionality;
import com.scalar.chiptrack.commons.SessionKeys;
import com.scalar.ScalarException;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionForm;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.HashMap;

/**
 * Created by ChandraSekahr.
 * User: Administrator
 * Date: Nov 17, 2005
 * Time: 8:41:04 PM
 */
public class ShippingRequestSaveCommand extends ActionCommand
{
    private static final String ACTION_NAME = "/shippingRequest.do";
    public ActionForward execute( ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response ) throws ScalarException
    {
        ShippingRequestForm shippingRequestForm     = ( ShippingRequestForm ) form;
        ShippingRequest shippingRequest             = shippingRequestForm.getShippingRequest();
        ActionForward actionForward                 = mapping.findForward( "confirmTransfer" );;
        HttpSession mSession                        = request.getSession();
        User user                                   = ( User ) mSession.getAttribute ( SessionKeys.USER_KEY );
        HashMap errors                              = new HashMap();
        HashMap info                                = new HashMap();
        request.setAttribute( INFO_MESSAGES, info );
        request.setAttribute( ERROR_MESSAGES, errors );

        if ( user == null )
        {
            mSession.setAttribute ( SessionKeys.SOURCE_PAGE_KEY,  ACTION_NAME );
            return ( mapping.findForward ( "login" ) );
        }
        else
        {
            if ( user.isActionAllowed ( Functionality.SORT_RELEASE , User.USER_ACTION_MODIFY ) )
            {
                request.setAttribute ( "showMessage", "FALSE" );
            }
            else
            {
                request.setAttribute ( "showMessage", "TRUE" );
            }
        }

        try
        {
            request.setAttribute( "isConfirm", "true" );
            actionForward = mapping.findForward( "confirmTransfer" );
        }
        catch( Exception e )
        {
            errors.put( "Could not save the Lot Information", null );
        }
		if( !errors.isEmpty() )
		{
			request.setAttribute( SessionKeys.ERROR_MESSAGES, errors );
		}

		if( !info.isEmpty() )
		{
			request.setAttribute( SessionKeys.INFO_MESSAGES, info );
		}

	   return actionForward;
    }

}
